import Common from '../../common/common.js';
import MessList from "./MessList.js";
import {bigEmoji, emojiMap, emojiName, emojiUrl} from "../../common/emojiMap.js";
import TimNet from '../../timnet.js';

const doms = {
    talkTitle:Common.tool.getElementByID("talkTitle"),
    messList:Common.tool.getElementByID("messList"),
    sendMess:Common.tool.getElementByID("sendMess"),
    textarea:Common.tool.getElementByID("textarea"),
    bigEmojiBox:Common.tool.getElementByID("bigEmojiBox"),
    emojiBox:Common.tool.getElementByID("emojiBox"),
    tools:{
        toolsBigEmoji:Common.tool.getElementByID("toolsBigEmoji"),
        toolsEmoji:Common.tool.getElementByID("toolsEmoji"),
        toolsFile:Common.tool.getElementByID("toolsFile"),
        toolsImage:Common.tool.getElementByID("toolsImage"),
        toolsVideo:Common.tool.getElementByID("toolsVideo")
    },
    picker:{
        imagePicker:Common.tool.getElementByID("imagePicker"),
        filePicker:Common.tool.getElementByID("filePicker"),
        videoPicker:Common.tool.getElementByID("videoPicker")
    }
}

const messList = new MessList();
doms.messList.appendChild(messList.getElement());

//绑定滚动事件
Common.tool.addHandle(doms.messList,"scroll",(e)=>{
   if(doms.messList.scrollTop == 0){
       messList.loadMessageList(false);
   }
});

//绑定发信息按钮
Common.tool.addHandle(doms.sendMess,"click",(e)=>{
    let text = doms.textarea.value;
    doms.textarea.value = "";
    //发送文本信息
    messList.sendTextMess(text);
});


//初始化表情
emojiName.forEach(v => {
    const emoji = Common.tool.createElement({
        label: "img",
        props: {
            src: emojiUrl + emojiMap[v],
            style: "width:20px;height:20px;margin:2px;cursor: pointer;",
            name: v
        }
    });
    doms.emojiBox.appendChild(emoji);
    //绑定事件
    Common.tool.addHandle(emoji,"click",()=>{
        doms.textarea.value += emoji.name;
    })
});

//初始化大表情
bigEmoji.forEach((v,i) => {
    const emoji = Common.tool.createElement({
        label: "img",
        props: {src: v,index:i, style: "width:50px;height:50px;margin:2px;cursor: pointer;"}
    });
    doms.bigEmojiBox.appendChild(emoji);
    //绑定事件
    Common.tool.addHandle(emoji,"click",()=>{
         messList.sendFaceMess(emoji.index);
    });
})

//绑定显示表情区域事件
let emojiBoxStatus = false;
let bigEmojiBoxStatus = false;
const changeEmojiDisplay = ()=>{
    doms.bigEmojiBox.style.display = (bigEmojiBoxStatus?"block":"none");
    doms.emojiBox.style.display = (emojiBoxStatus?"block":"none");
}
Common.tool.addHandle(doms.tools.toolsEmoji,"click",()=>{
    emojiBoxStatus = !emojiBoxStatus;
    bigEmojiBoxStatus = false;
    changeEmojiDisplay();
});
Common.tool.addHandle(doms.tools.toolsBigEmoji,"click",()=>{
    bigEmojiBoxStatus = !bigEmojiBoxStatus;
    emojiBoxStatus = false;
    changeEmojiDisplay();
});

//绑定打开选择文件
Common.tool.addHandle(doms.tools.toolsFile,"click",()=>{
    doms.picker.filePicker.value = "";
    doms.picker.filePicker.click();
});
//发送文件
Common.tool.addHandle(doms.picker.filePicker,"change",()=>{
    messList.sendFileMess(doms.picker.filePicker);
});

//绑定打开选择图片
Common.tool.addHandle(doms.tools.toolsImage,"click",()=>{
    doms.picker.imagePicker.value = "";
    doms.picker.imagePicker.click();
});

//发送图片
Common.tool.addHandle(doms.picker.imagePicker,"change",()=>{
    messList.sendImageMess(doms.picker.imagePicker);
});


//绑定打开选择视频
Common.tool.addHandle(doms.tools.toolsVideo,"click",()=>{
    doms.picker.videoPicker.value = "";
    doms.picker.videoPicker.click();
});

//发送视频
Common.tool.addHandle(doms.picker.videoPicker,"change",()=>{
    messList.sendVideoMess(doms.picker.videoPicker);
});


Common.tool.addHandle(window.document,"mousedown",function(e){
    if(window.messControls){
        if(e.target.className.indexOf("messControls") == -1 &&  e.target.parentElement.className.indexOf("messControls") == -1){
            window.messControls.cancel();
            window.messControls = false;
        }
    }
});

const sendErrorMess = function(text){
    messList.sendError(text)
}
const sendInfoMess = function(text){
    messList.sendInfo(text)
}

//信息回复
const messReceived = function(data){
    messList.pushMess(data)
}

//打开会话
const open = function(conversation){

    let title;
    if(conversation.type == TIM.TYPES.CONV_C2C){
        title = conversation.userProfile.nick;
        title = Common.tool.isNull(title)?conversation.userProfile.userID:title;
    }else if(conversation.type == TIM.TYPES.CONV_GROUP){
        title = conversation.groupProfile.name;
    }else{
        title = "系统通知";
    }
     doms.talkTitle.innerText = title;
    messList.resetByConversation(conversation);
    TimNet.setMessageRead(conversation.conversationID)
}

const openByID = function(conversationID){
   TimNet.getConversationProfile(conversationID,(data)=>{
       open(data);
   })
}

//a关闭会话
const close = function(){
    messList.clearItems();
}



export default {
    sendErrorMess:sendErrorMess,
    sendInfoMess:sendInfoMess,
    open:open,
    openByID:openByID,
    messReceived:messReceived,
    close:close
};
